package nio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.channels.FileChannel;
import java.security.SecureRandom;

/**
 * NIO的几个简单的 demo
 */

public class NioDemo1 {
    public static void main(String[] args) throws Exception{
        demo4();
    }

    private static void demo1(){
        IntBuffer buffer = IntBuffer.allocate(10);
        System.out.println("---------------buffer初始化--------------------");
        logBufferInfo(buffer);
        System.out.println("---------------开始写入--------------------");
        for (int i = 0; i < buffer.capacity()/2; i++) {
            int randomInt = new SecureRandom().nextInt(10);
            buffer.put(randomInt);
            logBufferInfo(buffer);
        }
        System.out.println("---------------写入完成--------------------");
        System.out.println("---------------flip--------------------");
        buffer.flip();
        logBufferInfo(buffer);

        System.out.println("---------------开始写出--------------------");
        while (buffer.hasRemaining()){
            buffer.get();
            logBufferInfo(buffer);
        }
        System.out.println("---------------clear--------------------");
        buffer.clear();
        logBufferInfo(buffer);
        System.out.println("---------------开始写入--------------------");
        for (int i = 0; i < buffer.capacity(); i++) {
            int randomInt = new SecureRandom().nextInt(10);
            buffer.put(randomInt);
            logBufferInfo(buffer);
        }
    }

    private static void demo2() throws Exception{
        FileInputStream fileInputStream = new FileInputStream("niodemo1.txt");
        FileChannel fileChannel = fileInputStream.getChannel();

        ByteBuffer buffer = ByteBuffer.allocate(512);
        fileChannel.read(buffer);

        buffer.flip();

        while (buffer.hasRemaining()){
            byte b = buffer.get();
            new String();
            System.out.println((char)b);
        }
    }

    private static void demo3() throws Exception{
        FileOutputStream outputStream = new FileOutputStream("niodemo2.txt");
        FileChannel fileChannel = outputStream.getChannel();

        ByteBuffer buffer = ByteBuffer.allocate(512);
        buffer.put("hello nio!".getBytes());

        buffer.flip();

        fileChannel.write(buffer);
    }

    private static void demo4() throws Exception{
        ByteBuffer buffer = ByteBuffer.allocate(10);

        buffer.putChar('牛');
        buffer.putFloat(1.2f);
        buffer.putInt(10);

        buffer.flip();

        char a = buffer.getChar();
        float b = buffer.getFloat();
        int c = buffer.getInt();

        System.out.println(a);
        System.out.println(b);
        System.out.println(c);

    }

    private static void logBufferInfo(Buffer buffer){
        int limit = buffer.limit();
        int position = buffer.position();
        int capacity = buffer.capacity();

        System.out.println("["+"limit:"+limit+",position:"+position+",capacity:"+capacity+"]");
    }
}
